package com.redstars.generate.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redstars.generate.entity.SysProject;
import com.redstars.generate.entity.SysProjectConfig;
import com.redstars.generate.enums.ProjectDefaultEnum;
import com.redstars.generate.exception.RedstarsException;
import com.redstars.generate.page.PageResult;
import com.redstars.generate.service.SysProjectService;
import com.redstars.generate.utils.PageUtils;
import com.redstars.generate.vo.SysProjectVo;
import lombok.AllArgsConstructor;
import com.redstars.generate.convert.SysProjectConfigConvert;
import com.redstars.generate.query.SysProjectConfigQuery;
import com.redstars.generate.vo.SysProjectConfigVo;
import com.redstars.generate.dao.SysProjectConfigDao;
import com.redstars.generate.service.SysProjectConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;

import javax.annotation.Resource;

/**
 * 项目基础配置
 *
 * @author zhouhx 
 * @since 1.0.0 2022-10-14
 */
@Service
@AllArgsConstructor
public class SysProjectConfigServiceImpl extends ServiceImpl<SysProjectConfigDao, SysProjectConfig> implements SysProjectConfigService {
    @Resource
    SysProjectService projectService;

    @Override
    public PageResult<SysProjectConfigVo> page(SysProjectConfigQuery query) {
        LambdaQueryWrapper<SysProjectConfig> wrapper = getWrapper(query);
        PageUtils<SysProjectConfig> pageUtils = new PageUtils<>();
        IPage<SysProjectConfig> page = this.page(pageUtils.getPage(query), wrapper);

        return pageUtils.getPageData(page, SysProjectConfigVo.class);
    }

    private LambdaQueryWrapper<SysProjectConfig> getWrapper(SysProjectConfigQuery query){
        LambdaQueryWrapper<SysProjectConfig> wrapper = Wrappers.lambdaQuery();

        return wrapper;
    }

    @Override
    public void save(SysProjectConfigVo vo) {
        SysProjectConfig entity = SysProjectConfigConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SysProjectConfigVo vo) {
        SysProjectConfig entity = SysProjectConfigConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }
    @Override
    public SysProjectConfigVo detail(Long id) {
        SysProjectConfig detail = this.getById(id);
        if (ObjectUtil.isEmpty(detail)) {
        throw new RedstarsException("记录不存在");
        }
        return BeanUtil.copyProperties(detail,SysProjectConfigVo.class);
    }

    @Override
    public SysProjectConfigVo detailByProjectId(Long projectId) {
        LambdaQueryWrapper<SysProjectConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysProjectConfig::getSysProjectId,projectId);
        SysProjectConfig res = this.getOne(queryWrapper);
        return BeanUtil.copyProperties(res,SysProjectConfigVo.class);
    }

    @Override
    public SysProjectConfigVo getDefalut() {
        SysProjectVo project = projectService.getDefalut();
        if(ObjectUtil.isNotEmpty(project)){
           return  this.detailByProjectId(project.getId());
        }
        return new SysProjectConfigVo();
    }

}